package myfunc;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import basis.DbConnect;

/**
 * Servlet implementation class myfind
 */
public class myfind_train extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public myfind_train() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Vector<String> up=new Vector();
		Vector<String> down=new Vector();
		try {	
			request.setCharacterEncoding("UTF-8");
			response.setContentType("text/html;charset=utf-8");
			String value=request.getParameter("address");
			Connection con=DbConnect.getConnection();
			String sql ="select up,down from trainget where startaddress=?";
			PreparedStatement pstmt=con.prepareStatement(sql);
			pstmt.setString(1, value);
			pstmt.executeQuery();
			ResultSet rst=pstmt.getResultSet();
			String s="{'d':[";
			if(rst.next()) {				
				String uptrain=rst.getString(1);
				String downtrain=rst.getString(2);
				String para1[];
				String para2[];
				if(uptrain!=null) {
					para1=uptrain.split(",");
					for(int i=0;i<para1.length;i++) {
						up.add(para1[i]);
					}
				}
				if(downtrain!=null) {
					para2=downtrain.split(",");
					for(int i=0;i<para2.length;i++) {
						down.add(para2[i]);
					}
				}
				System.out.println(up+"-----"+down);
				rst.close();
				pstmt.close();
				for(int i=0;i<up.size();i++) {
					String key=up.get(i);
					sql="select *from trainbasis where trainno=?";
					PreparedStatement Realpstmt=con.prepareStatement(sql);
					Realpstmt.setString(1, key);
					Realpstmt.executeQuery();
					ResultSet Realrst=Realpstmt.getResultSet();
				if(Realrst.next()) {
					s+="{'d2':[";
					for(int j=1;j<=8;j++) {
						s+="'"+Realrst.getString(j)+"',";
					}
					s+="]},";
				}
				Realrst.close();
				Realpstmt.close();
			}
			for(int i=0;i<down.size();i++) {
				String key=down.get(i);
				sql="select *from trainbasis where trainno=?";
				PreparedStatement Realpstmt=con.prepareStatement(sql);
				Realpstmt.setString(1, key);
				Realpstmt.executeQuery();
				ResultSet Realrst=Realpstmt.getResultSet();
				if(Realrst.next()) {
					s+="{'d2':[";
					for(int j=1;j<=8;j++) {
						s+="'"+Realrst.getString(j)+"',";
					}
					s+="]},";
				}
				Realrst.close();
				Realpstmt.close();
			}
			}
			s+="]}";
			System.out.println(s);
			PrintWriter out=response.getWriter();
			out.print(s);
			con.close();
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
